# -------------------------------------------------------------------
# Purpose: Creates Table B24
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))


# Load data
load(file.path(pdataconfanalysis, "inventorLevel19001940.RData"))


# Regressions
o <- list()
o <- append(o, list(feols(sum_patents_f_w ~ log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))
o <- append(o, list(feols(sum_patents_f_w ~ entropy_evenness_namelast_mp_adjp_ws + log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))
o <- append(o, list(feols(sum_patents_f_w ~ entropy_namelast_mp_adjp_ws + entropy_evenness_namelast_mp_adjp_ws + log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))
o <- append(o, list(feols(sum_break_p80_rrfsim05_f_w ~ log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))
o <- append(o, list(feols(sum_break_p80_rrfsim05_f_w ~ entropy_evenness_namelast_mp_adjp_ws + log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))
o <- append(o, list(feols(sum_break_p80_rrfsim05_f_w ~ entropy_namelast_mp_adjp_ws + entropy_evenness_namelast_mp_adjp_ws + log_no_of_namelast_mp_adjp_s + sum_n_namelast_mp_adjp_ws | inv_id + year^statefip, inventorLevel19001940)))


# Create table
y1_mean <- round(mean(inventorLevel19001940$sum_patents_f_w), 2)
y1_sd <- round(sd(inventorLevel19001940$sum_patents_f_w), 2)
y2_mean <- round(mean(inventorLevel19001940$sum_break_p80_rrfsim05_f_w), 2)
y2_sd <- round(sd(inventorLevel19001940$sum_break_p80_rrfsim05_f_w), 2)
y1 <- paste0("\\makecell{Patents \\\\ (mean = ", y1_mean, ", sd = ", y1_sd, ")}")
y2 <- paste0("\\makecell{Breakthrough patents \\\\ (mean = ", y2_mean, ", sd = ", y2_sd, ")}")

setFixest_dict(
  c(
    entropy_namelast_mp_adjp_ws = "Surname diversity",
    log_no_of_namelast_mp_adjp_s = "Log distinct surnames",
    sum_n_namelast_mp_adjp_ws = "Population",
    entropy_evenness_namelast_mp_adjp_ws = "Surname dispersion",
    sum_patents_f_w = y1, sum_break_p80_rrfsim05_f_w = y2,
    year = "Period", statefip = "State", gisjoin_1900 = "County", inv_id = "Inventor"
  )
)

tablename <- file.path(poutputappendix, "tableB26.tex")
etable(o,
  cluster = ~statefip,
  fitstat = ~n,
  order = c("diversity", "distinct", "dispersion"),
  digits = "r3", digits.stats = "r3",
  file = tablename, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
add_table_row(tablename, "mean =", "\\cmidrule(lr){2-4}  \\cmidrule(lr){5-7}")
move_table_row(tablename, "Observations", "bottomrule")
writeLines(readLines(tablename)[-18], tablename)

cat("Table B26 saved to:", tablename, "\n")